﻿using SCS.Common.Models;
using SCS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SCS.Business
{
    public class ViewGradesByClassServices
    {
        IUnitOfWork _unitOfWork;
        //Dbcontext dbcontext = new Dbcontext();
        public ViewGradesByClassServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// GetAllSchoolYear
        /// </summary>
        /// <returns>school year list</returns>
        public IQueryable<SchoolYear> GetAllSchoolYear()
        {
            return _unitOfWork.SchoolYear.All();
        }

        /// <summary>
        /// find
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>school year</returns>
        public SchoolYear Find(int id)
        {
            return _unitOfWork.SchoolYear.Find(id);
        }

        /// <summary>
        /// ViewGradesByClass
        /// </summary>
        /// <returns>grade list</returns>
        public IList<Grade> ViewGradesByClass()
        {
            return _unitOfWork.Grade.GetAll().ToList();
        }

        /// <summary>
        /// GetClassListInViewGradesByClass
        /// </summary>
        /// <returns>class list</returns>
        public IEnumerable<ClassInSchoolYear> GetClassListInViewGradesByClass(int schoolYearsID,string userName)
        {
            MembershipUser user = _unitOfWork.MembershipUser.GetUser(userName);
            int teacherID = 0;
            if (user.Teachers.Any())
            {
                teacherID = user.Teachers.FirstOrDefault().teacherID;
            }
            return _unitOfWork.ClassInSchoolYear.Get(m => m.Class.TeachingAssignments.Where(t => t.schoolYearID == schoolYearsID && t.teacherID == teacherID).Count() > 0 && m.schoolYearID == schoolYearsID);
        }

        /// <summary>
        /// getSubjectList
        /// </summary>
        /// <param name="schoolYearID">schoolYearID</param>
        /// <param name="classID">classID</param>
        /// <returns>teaching assign list</returns>
        public IEnumerable<TeachingAssignment> getSubjectList(int? schoolYearID, int? classID)
        {
            return _unitOfWork.TeachingAssignment.Get(m => m.classID == classID && m.schoolYearID == schoolYearID);
        }

        /// <summary>
        /// ViewGradesByClass
        /// </summary>
        /// <param name="schoolYearID">schoolYearID</param>
        /// <param name="termID">termID</param>
        /// <param name="classID">classID</param>
        /// <param name="subjectID">subjectID</param>
        /// <returns>grade list</returns>
        public IList<Grade> ViewGradesByClass(int? schoolYearID, int? termID, int? classID, int? subjectID)
        {
            return _unitOfWork.Grade.Get(m => m.Term.schoolYearID == schoolYearID
                                           && m.termID == termID
                                           && m.Learner.classID == classID
                                           && m.subjectID == subjectID).ToList();
        }
    }
}
